package org.example.mall.admin.controller.manage;


import cn.hutool.core.lang.tree.Tree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.example.mall.admin.model.enums.manage.MenuTypeEnum;
import org.example.mall.admin.model.entity.manage.Menu;
import org.example.mall.admin.service.manage.MenuService;
import org.example.mall.admin.util.BusOp;
import org.example.mall.admin.util.R;
import org.example.mall.admin.util.SecurityUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author hzq
 * @since 2021-12-29
 */
@Api(tags = "菜单管理")
@RestController
@AllArgsConstructor
@RequestMapping("/manage/menu")
public class MenuController {

    private final MenuService menuService;

    /**
     * 返回当前用户的树形菜单集合
     *
     * @param type     类型
     * @param parentId 父节点ID
     * @return 当前用户的树形菜单
     */
    @GetMapping("/userTree")
    @ApiOperation("返回当前用户的树形菜单集合")
    public R<List<Tree<Integer>>> userTree(String type, Integer parentId) {
        Set<Menu> menus = new HashSet<>(menuService.accountMenus(SecurityUtil.getAuth().getAccountId(), SecurityUtil.getClientId()));
        return R.ofSuccess(menuService.filterMenu(menus, type, parentId));
    }

    /**
     * 返回客户端树形菜单集合
     *
     * @param clientId 客户端
     * @param lazy     是否是懒加载
     * @param parentId 父节点ID
     * @return 树形菜单
     */
    @GetMapping(value = "/clientTree/{clientId}")
    @ApiOperation("返回客户端树形菜单集合")
    public R<List<Tree<Integer>>> clientTree(@PathVariable String clientId, boolean lazy, Integer parentId) {
        return R.ofSuccess(menuService.treeMenu(clientId, lazy, parentId));
    }

    /**
     * 返回角色的菜单集合
     *
     * @param roleId 角色ID
     * @return 属性集合
     */
    @GetMapping("/roleTree/{roleId}")
    @ApiOperation("返回角色的菜单集合")
    public R<List<Integer>> roleTree(@PathVariable Integer roleId) {
        return R.ofSuccess(menuService.findMenusByRoleId(roleId).stream()
                .map(Menu::getMenuId).collect(Collectors.toList()));
    }

    /**
     * 通过ID查询菜单的详细信息
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @GetMapping("/{id}")
    @ApiOperation("通过ID查询菜单的详细信息")
    public R<Menu> getById(@PathVariable Integer id) {
        return R.ofSuccess(menuService.getById(id));
    }

    /**
     * 新增菜单
     *
     * @param menu 菜单信息
     */
    @PostMapping
    @ApiOperation("新增菜单")
    @PreAuthorize("hasAuthority('menu_add')")
    public R<Menu> save(@Valid @RequestBody Menu menu) {
        checkMenu(menu);
        BusOp.mustSucceed(menuService.save(menu), "新增菜单失败");
        return R.ofSuccess("新增菜单成功", menu);
    }

    /**
     * 检验
     *
     * @param menu 菜单
     */
    private void checkMenu(Menu menu) {
        if (menu.getType().equals(MenuTypeEnum.BUTTON.getType())) {
            menu.setPath(null);
        } else {
            menu.setPermission(null);
        }
    }

    /**
     * 删除菜单
     *
     * @param id 菜单ID
     * @return success/false
     */
    @ApiOperation("删除菜单")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('menu_del')")
    public R<Boolean> removeById(@PathVariable Integer id) {
        return R.ofSuccess("删除菜单成功",
                BusOp.mustSucceed(menuService.removeMenuById(id), "删除菜单失败"));
    }

    /**
     * 更新菜单
     *
     * @param menu 菜单信息
     * @return true/false
     */
    @PutMapping
    @ApiOperation("更新菜单")
    @PreAuthorize("hasAuthority('menu_edit')")
    public R<Boolean> update(@Valid @RequestBody Menu menu) {
        checkMenu(menu);
        return R.ofSuccess("更新菜单成功",
                BusOp.mustSucceed(menuService.updateById(menu), "更新菜单失败"));
    }

}

